//
//  GetGoodListVm.swift
//  Pods
//
//  Created by eme on 2017/3/30.
//
//

import RxSwift
import SwiftyJSON
import NetWorkCore
import ModelProtocol
import RickonCore
import UtilCore

class GetGoodListVm:Base_Vm{
    
    let backDate = Variable<[CollectGoodsListModel]>([])
//    let disposeBag = DisposeBag()
    /// 错误
//    let error = PublishSubject<MikerError>()
    
    let refreshing = Variable(false)
    
    let page = Variable(1)
    
    let hasNextPage = Variable(true)
    
    var getCollectGoodsList = PublishSubject<Void>()
    
    /// 表示正在加载更多种..
    let loading : Observable<Bool>
    
    init (
        
        input:(
        refreshTriger: Observable<Void>,
        loadMoreTriger: Observable<Void>
        )
        ){
        let requestIndicator = ActivityIndicator()
        self.loading = requestIndicator.asObservable()
        
        super.init()
        
        
        let refreshRequest = (
            input.refreshTriger
                .map{ _ in
                    return 1
                }
                .map{ page in
                    return (page - 1) * AppConfig.pageSize
        }).map{ page in
            return MineApi.getCollectGoodsList(page: page, length: AppConfig.pageSize)
            }.shareReplay(1)
        
        let refreshResponse = refreshRequest.emeRequestApiForArray(CollectGoodsListModel.self, activityIndicator: requestIndicator).shareReplay(1)
        
        
        refreshResponse.subscribe(onNext:{[unowned self] result  in
            switch result {
            case .success(let data):
                
                self.hasNextPage.value = data.count == AppConfig.pageSize
                self.backDate.value = data
                
            case .failure(let error):
                self.error.onNext(error)
                
            }
            
        }).addDisposableTo(disposeBag)
        
        let loadMoreRequest = (
            input.loadMoreTriger
            ).map{ _ in
                return MineApi.getCollectGoodsList(page: self.page.value - 1, length: AppConfig.pageSize)
            }.shareReplay(1)
        
        let loadMoreResponse = loadMoreRequest.emeRequestApiForArray(CollectGoodsListModel.self)
            .shareReplay(1)
        
        loadMoreResponse.subscribe(onNext:{[unowned self] result  in
            switch result {
            case .success(let data):
                
                
                self.hasNextPage.value = data.count == AppConfig.pageSize
                
                self.backDate.value += data
                /**
                 *  需要修改page 页数
                 */
                self.page.value += 1
                
            case .failure(let error):
                
                self.error.onNext(error)
                
            }
            
        }).addDisposableTo(disposeBag)
        
        
        refreshRequest.subscribe(onNext:{ _ in
            
            self.hasNextPage.value = true
            self.page.value = 2
            
            }).addDisposableTo(disposeBag)
        
        let refreshRequest_first = self.getCollectGoodsList.map{ _ in
            return MineApi.getCollectGoodsList(page: 0, length: AppConfig.pageSize)
            }.shareReplay(1)
        
        let refreshResponse_first = refreshRequest_first.emeRequestApiForArray(CollectGoodsListModel.self, activityIndicator: requestIndicator).shareReplay(1)
        
        
        refreshResponse_first.subscribe(onNext:{[unowned self] result  in
            switch result {
            case .success(let data):
                
                self.hasNextPage.value = data.count == AppConfig.pageSize
                self.backDate.value = data
                
            case .failure(let error):
                self.error.onNext(error)
                
            }
            
        }).addDisposableTo(disposeBag)
        
        
        Observable
            .of(
                refreshRequest.map { _ in true },
                refreshResponse.map { _ in false },
                error.map { _ in false }
            )
            .merge()
            .bindTo(refreshing)
            .addDisposableTo(disposeBag)
        
        
    }
    
}
